
import { KEYWORDS, BASE_URL, UPLOADFILE_URL } from '@/common/config/config.ts';

/** 
 * @description 接口请求 requestUrl({url,method, data,keyWord, success,  header,loading })
 * @param {String} url
 * @param {String} method
 * @param {String} keyWord //关键字 区分请求地址
 * @param {Object} data //参数
 **/
export const requestUrl = ({ url, method, data, keyWord, isJson = false, }) => {
	let contentType: string = 'application/x-www-form-urlencoded';
	if (isJson) {
		contentType = 'application/json';
	} else {
		contentType = 'application/x-www-form-urlencoded';
	}
	let keywordStr = '/';
	if (keyWord) {
		keywordStr = '/' + KEYWORDS[keyWord];
	}
	uni.showLoading({
		mask: true,
		title: '加载中',
	});
	return new Promise(function(resolve, reject) {
		let Authorization = null;
		try {
			//同步获取缓存
			const token = uni.getStorageSync('tokenInformation');
			if (token) {
				Authorization = token.tokenStrWithType
			} else {
				if (!url.includes('login')) {
					reject(token);
					uni.showToast({
						icon: 'none',
						title: '登陆已过期！',
						duration: 500
					});
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}, 500)
					return false
				}
			}
		} catch (e) {
			console.log(e);
			reject(e);
			return false
		}
		uni.request({
			url: BASE_URL + keywordStr + url,
			method: method,
			data: data,
			header: {
				'content-type': contentType,
				'Authorization': Authorization
			},
		})
			.then((res: any) => {
				uni.hideLoading();
				// 此处的 res 参数，与使用默认方式调用时 success 回调中的 res 参数一致
				// console.log('111',res);
				if (res.data.code === 0) {
					resolve(res.data)
				} else if (res.data.code === 13) {
					//token失效
					uni.clearStorageSync();
					uni.showToast({
						icon: 'none',
						title: '登陆已过期！',
						duration: 500
					});
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}, 500)
					reject(res.data);
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.message,
					});
					resolve(res.data);
				}

			})
			.catch((err) => {
				uni.hideLoading();
				// 此处的 err 参数，与使用默认方式调用时 fail 回调中的 err 参数一致
				// console.error('2222',err);
				reject(err)
			});

	});
};




/**
 * @param filesPath  上传入参对象

 */
export const batchUpload = (filesPath: any[]) => {
	let imgList: any[] = filesPath.map((value, index) => {
		return {
			name: 'file' + index,
			uri: value,
		}
	})
	uni.showLoading({
		mask: true,
		title: '加载中',
	});
	return new Promise(function(resolve, reject) {
		//添加token验证
		let Authorization = null;
		try {
			//同步获取缓存
			const token = uni.getStorageSync('tokenInformation');
			if (token) {
				Authorization = token.tokenStrWithType
			} else {
				uni.showToast({
					icon: 'none',
					title: '登陆已过期！',
					duration: 500
				});
				setTimeout(() => {
					uni.hideLoading();
					uni.redirectTo({
						url: '/pages/login/login'
					})
				}, 500)
				return false
			}
		} catch (e) {
			console.log(e);
			reject(e);
			return false
		}
		uni.uploadFile({
			url: UPLOADFILE_URL,
			files: imgList,
			fileType: 'image',
			formData: { saveMode: 1, storageType: 'LOCAL' },//上传文件的额外参数 接口必传
			header: {
				'Authorization': Authorization
			},//添加token
			success(res) { // 返回code为指定结果表示上传成功反之上传失败
				uni.hideLoading();
				if (res.data.code === 0) {
					resolve(res.data)
				} else if (res.data.code === 13) {
					//token失效
					uni.clearStorageSync();
					uni.showToast({
						icon: 'none',
						title: res.data.message,
						duration:500
					});
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}, 500)
					reject(res.data);
				} else {
					resolve(res.data);
				}
			},
			fail(err) {
				uni.hideLoading();
				reject(err);
			}
		})

	});
};

//获取顶部高度
export const getTopHeight = () => {
	const res = uni.getSystemInfoSync();
	const statusBarHeight = res.statusBarHeight; //状态栏的高度 
	const navigationBarHeight = res.system == 'android' ? 48 : 44;
	return {
		statusBarHeight,
		navigationBarHeight
	};
};


